
#ifndef TEXTUREMANAGER_H_
#define TEXTUREMANAGER_H_

#include "terrain_pcx.h"

sImage pcx;
u16* gfx [256];

int TERRAIN_TEXTURE [1];

void initGFX() {
	for(int i = 0; i < 128; i++) {
		gfx[i] = oamAllocateGfx(&oamSub, SpriteSize_16x16, SpriteColorFormat_256Color);
		gfx[i+128] = oamAllocateGfx(&oamMain, SpriteSize_16x16, SpriteColorFormat_256Color);
	}
}

void loadUsePcx(u8* data8) {
	loadPCX(data8, &pcx);
	imageTileData(&pcx);
}

void destroyUsePcx() {
	imageDestroy(&pcx);
}

void loadPcxTexture(u8* data8, int* t) {
	sImage pcx3d;
	loadPCX(data8, &pcx3d);
	image8to16(&pcx3d);
	glGenTextures(1, &t[0]);
	glBindTexture(GL_TEXTURE_2D, t[0]);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, TEXTURE_SIZE_256, TEXTURE_SIZE_256, 0, TEXGEN_TEXCOORD, pcx3d.image.data8);
	imageDestroy(&pcx3d);
}

void loadPngTexture(u8* data8, int* t) {
	glGenTextures(1, &t[0]);
	glBindTexture(GL_TEXTURE_2D, t[0]);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, TEXTURE_SIZE_256, TEXTURE_SIZE_256, 0, TEXGEN_TEXCOORD, data8);
}

void loadPcxToMain(u8* data8) {
	loadUsePcx(data8);

	for(int i = 0; i < 256; i++) {
		SPRITE_PALETTE[i] = pcx.palette[i];
	}
}

void loadPcxToSub(u8* data8) {
	loadUsePcx(data8);

	for(int i = 0; i < 256; i++) {
		SPRITE_PALETTE_SUB[i] = pcx.palette[i];
	}
}

void LoadGLTextures()
{
	loadPcxTexture((u8*)terrain_pcx, TERRAIN_TEXTURE);
	loadPcxToMain((u8*)terrain_pcx);
	loadPcxToSub((u8*)terrain_pcx);
}

#endif /* TEXTUREMANAGER_H_ */
